package com.lgy.rabbit.config;

import com.lgy.rabbit.mq.DemoRabbit;
import com.lgy.rabbit.mq.DlxBusinessRabbit;
import com.lgy.rabbit.mq.EmailTopicRabbit;
import com.lgy.rabbit.mq.OrderFanoutRabbit;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    public final static String SEND_MSM_QUEUE = "demo.send.sms.queue";

    public final static String SEND_MSM_FANOUT = "demo.order.fanout";

    public final static String EMAIL_TOPIC_QUEUE = "email.topic.queue";

    @Bean
    public Queue orderFanoutQuery() {
        return new Queue(SEND_MSM_FANOUT, true);
    }
    @Bean
    public FanoutExchange orderExchange() {
        return new FanoutExchange(OrderFanoutRabbit.DEMO_SEND_FANOUT.exchange(), true, false);
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderFanoutQuery())
                .to(orderExchange());

        //return BindingBuilder.bind(orderTopicQuery())
          //      .to(orderExchange())
    }

    @Bean
    public Queue smsSendQueue() {
        return new Queue(SEND_MSM_QUEUE, true);
    }
    /**
     * 短信消息实际消费队列所绑定的交换机
     */
    @Bean
    public Exchange smsExchange() {
        return new DirectExchange(DemoRabbit.DEMO_SEND.exchange(), true, false);
    }

    @Bean
    public Binding smsSendBinding() {
        return BindingBuilder.bind(smsSendQueue())
                            .to(smsExchange())
                            .with(DemoRabbit.DEMO_SEND.routingKey())
                            .noargs();
    }

    //----------------------------------------------------- TOPIC -----------------------------------------------
    @Bean
    public Queue emailTopicQueue() {
        return new Queue(EMAIL_TOPIC_QUEUE, true);
    }
    /**
     * 短信消息实际消费队列所绑定的交换机
     */
    @Bean
    public Exchange emailTopicExchange() {
        return new TopicExchange(EmailTopicRabbit.EMAIL_TOPIC_RABBIT.exchange(), true, false);
    }

    @Bean
    public Binding emailTopicBinding() {
        return BindingBuilder.bind(emailTopicQueue())
                .to(emailTopicExchange())
                .with(EmailTopicRabbit.EMAIL_TOPIC_RABBIT.routingKey())
                .noargs();
    }

    //----------------------------------------------------- DlX-Queue -----------------------------------------------
    public final static String USER_MESSAGE_DLX_QUEUE = "user.message.dlx.queue";
    public final static String USER_MESSAGE_NORMAL_QUEUE = "user.message.queue";
    @Bean
    public Queue userMessageQueue() {
        return QueueBuilder.durable(USER_MESSAGE_NORMAL_QUEUE)
                .withArgument("x-dead-letter-exchange", DlxBusinessRabbit.USER_MESSAGE_DEAD.exchange())      // 死信交换器
                .withArgument("x-dead-letter-routing-key", DlxBusinessRabbit.USER_MESSAGE_DEAD.routingKey()) // 死信路由键
                .build();
    }
    @Bean
    public Exchange userMessageExchange() {
        return new DirectExchange(DlxBusinessRabbit.USER_MESSAGE_NORMAL.exchange());
    }

    @Bean
    public Binding userMessageBinding() {
        return BindingBuilder.bind(userMessageQueue())
                             .to(userMessageExchange())
                             .with(DlxBusinessRabbit.USER_MESSAGE_NORMAL.routingKey())
                             .noargs();
    }


    // 死信队列
    @Bean
    public Queue duserMessageDlxQueue() {
        return new Queue(USER_MESSAGE_DLX_QUEUE,true);
    }

    // 死信交换器
    @Bean
    public DirectExchange userMessageDlxExchange() {
        return new DirectExchange(DlxBusinessRabbit.USER_MESSAGE_DEAD.exchange());
    }

    // 绑定死信队列
    @Bean
    public Binding userMessageDlxBinding() {
        return BindingBuilder.bind(duserMessageDlxQueue())
                             .to(userMessageDlxExchange())
                             .with(DlxBusinessRabbit.USER_MESSAGE_DEAD.routingKey());
    }
}
